use strict;
use utf8;
use Encode;

binmode(STDIN, ":encoding(utf-8)");
binmode(STDOUT, ":encoding(utf-8)");
binmode(STDERR, ":encoding(utf-8)");

my %categories;

while (<STDIN>) {
  chomp $_;
  $categories{$_} += 1;
}

if ($#ARGV < 0) {
  die "please specify categorylinks.sql file";
}

my $catlist_fn = $ARGV[0];
my $fh;

if ($catlist_fn =~ /.sql$/) {
  open($fh, "<$catlist_fn") || die "can't open $catlist_fn"; 
} elsif ($catlist_fn =~ /.sql.gz$/) {
  open($fh, "zcat $catlist_fn |") || die "can't open zcat $catlist_fn |"; 
} elsif ($catlist_fn =~ /.sql.bz2$/) {
  open($fh, "bzcat $catlist_fn |") || die "can't open bzcat $catlist_fn |"; 
} else {
  die "unknown extention $catlist_fn";
}

#binmode($fh, ":encoding(utf-8)");

while (<$fh>) {
  while ($_ =~ /\((\d+),\'([^\']+)\',\'([^\']+)\',\'([^\']+)\'/g) {
    my ($aid, $catname, $sortkey, $timestamp) = ($1, $2, $3, $4);
    $catname = decode("utf-8", $catname);
    print STDERR $catname . "\n";
    if (exists($categories{$catname}) && $sortkey ne "*") {
      print STDERR join(" ", $aid, $catname, $sortkey, $timestamp) . "\n";
      print $aid . "\n";
    }
  }
}
